/**
 * Created by Administrator on 2017/9/12/0012.
 */
define(function (require) {

    var popupDom = document.createElement("DIV");
    popupDom.classList.add("popup");
    var $ = require("jquery");
    require("jquery.cookie");
    var teacherHandler = new require("userHandler").TeacherHandler();
    var chatHandler = require("chatHandler");

    function createAddCoursePopup() {
        popupDom.innerHTML =
            '<div class="hover-icon"></div>' +
            '<h1>添加课程</h1>' +
            '<form><span>课程名</span><input id="popup__name"><br><br>' +
            '<span>课堂介绍</span><br> <textarea id="popup__introduce"></textarea>' +
            '<div class="button-group"><a id="popup__close"></a><a id="popup__submit"></a></div></form>';

        var closeDom = popupDom.querySelector("#popup__close"),
            submitDom = popupDom.querySelector("#popup__submit"),
            nameDom = popupDom.querySelector("#popup__name"),
            introduceDom = popupDom.querySelector("#popup__introduce");
        closeDom.addEventListener("click", function (e) {
            dispose();
        });
        submitDom.addEventListener("click", function (e) {
            teacherHandler.createCourse({
                name: nameDom.value,
                introduce: introduceDom.value
            }, function (data) {
                dispose();
                alert("创建成功");
                location.reload();
            }, function () {
                dispose();
                alert("创建失败")
            });
        });

        document.body.appendChild(popupDom);
    }


    function createEditCourseIntroducePopup() {
        popupDom.innerHTML =
            '<div class="hover-icon"></div>' +
            '<h1>添加课程</h1>' +
            '<form>' +
            '<span>课堂介绍</span><br> <textarea id="popup__introduce"></textarea>' +
            '<div class="button-group"><a id="popup__close"></a><a id="popup__submit"></a></div></form>';

        var closeDom = popupDom.querySelector("#popup__close"),
            submitDom = popupDom.querySelector("#popup__submit"),
            introduceDom = popupDom.querySelector("#popup__introduce");
        closeDom.addEventListener("click", function (e) {
            dispose();
        });
        submitDom.addEventListener("click", function (e) {
            teacherHandler.updateCourse($.cookie("courseId"), {
                introduce: introduceDom.value
            }, function (data) {
                dispose();
                alert("修改成功")
            }, function () {
                dispose();
                alert("修改失败")
            });
        });

        document.body.appendChild(popupDom);

    }

    function createSendMessagePopup() {
        popupDom.innerHTML =
            '<div class="hover-icon"></div>' +
            '<h1>讨论发言</h1>' +
            '<form>' +
            '<textarea id="popup__message"></textarea>' +
            '<div class="button-group"><a id="popup__close"></a><a id="popup__submit"></a></div></form>';

        var closeDom = popupDom.querySelector("#popup__close"),
            submitDom = popupDom.querySelector("#popup__submit"),
            messageDom = popupDom.querySelector("#popup__message");
        closeDom.addEventListener("click", function (e) {
            dispose();
        });


        submitDom.addEventListener("click", function () {

            chatHandler.send({
                    name: $.cookie("username"),
                    data: messageDom.value
                },
                function () {
                    dispose();
                    alert("发送成功")
                },
                function () {
                    dispose();
                    alert("发送失败")
                });
        })

        document.body.appendChild(popupDom);

    }

    function dispose() {
        document.body.removeChild(popupDom);
    }

    return {
        createAddCoursePopup: createAddCoursePopup,
        createEditCourseIntroducePopup: createEditCourseIntroducePopup,
        createSendMessagePopup: createSendMessagePopup,
        dispose: dispose
    }
})